/* 
  Copyright (c) SilverLining 2015
  Date:    2016-1-7
  File:    message.cpp  
  Description: 

*/

#include "stdafx.h"


using namespace m::alloc;
using namespace m::error;

namespace m
{
	m::message::message(process * sender, const wchar_t* message)
		: _Sender(nullptr),
		_Data(nullptr),
		_NextMessage(nullptr)
	{
		ref_storage track1;
		weak_ref* senderWeak = create_weak(track1, sender);
		assign(_Sender, senderWeak);
		wcscpy_s(_Type, message);
	}
	
	m::message::message(process * sender, const wchar_t* message, object * data)
		: _Sender(nullptr),
		_Data(nullptr),
		_NextMessage(nullptr)
	{
		if (sender)
		{
			ref_storage track1;
			weak_ref* senderWeak = create_weak(track1, sender);
			assign(_Sender, senderWeak);
		}
		assign(_Data, data);
		wcscpy_s(_Type, message);
	}

	m::message::~message()
	{
		clear(_Sender);
		clear(_Data);
	}

	weak_ref* m::message::sender() const
	{
		return _Sender;
	}
	
	object * m::message::data() const
	{
		return _Data;
	}
	
	const wchar_t * message::type() const
	{
		return _Type;
	}

	bool message::type_equals(const wchar_t * message) const
	{
		return _Type == message || wcscmp(_Type, message) == 0;
	}

	void * m::message::operator new(size_t sz)
	{
		return ::operator new(sz, pool);
	}
		
	void m::message::operator delete(void * ptr)
	{
		::operator delete(ptr, pool);
	}
}